package OI_test;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class inputsteam_test {
    public static void main(String[] args) throws IOException {
        /*InputStream input = null;
        try {
            input = new FileInputStream("study/src/user1.txt");
            int n;
            while ((n = input.read()) != -1) { // 利用while同时读取并判断
                System.out.print(n + " ");
            }
        }finally {
            if (input != null) { input.close();}
        }*/
        /*try(InputStream input = new FileInputStream("study/src/user1.txt")){
            int n;
            while((n = input.read()) != -1){
                System.out.print(n + " ");
            }
        }//用这种方式Java会帮我们自动写入finally并且调用close()方法*/


        try(InputStream input = new FileInputStream("study/src/user1.txt")){
            StringBuffer sb = new StringBuffer();
            int n;
            while((n = input.read()) != -1){
                //将读取出来的字节码强制转换为char类型的，将会显示出原本的文本
                sb.append((char) n);
                //中文乱码的问题：
                //一个汉字的字节是2-4字节
                //而空格是半个字节
                //缓冲区是12个字节，有可能是读取到了空格加半个汉字的字节，所以才会出现乱码
            }
            System.out.println(sb.toString());
        }
    }
}
